<template>
  <div class="actions-btn-group">
    <a-button type="primary" @click="toAddCollectForm">添加</a-button>
    <a-button type="primary" @click="toCollectLogList">采集日志</a-button>
    <a-button :disabled="btnVisible" @click="startOrDisabledHandle(2, '', '采集资源点')">启用</a-button>
    <a-button :disabled="btnVisible" @click="startOrDisabledHandle(1, '', '采集资源点')">禁用</a-button>
    <a-button danger :disabled="btnVisible" @click="handleDeleteCollect" class="mr-[20px]">删除</a-button>
  </div>
  <!-- 批量启用禁用 -->
  <ModalView
    title="提示"
    v-model:visible="modalTaskVisible"
    okText="确定"
    cancelText="取消"
    :width="458"
    @confirm="disableConfirm"
    @cancel="disableCancel"
  >
    <span>{{ `确定要${startOrDisabledBtnTextDefault}该采集资源点吗？` }}</span></ModalView
  >
  <!-- 批量删除 -->
  <ModalView
    title="提示"
    v-model:visible="deleteTaskVisible"
    okText="确定"
    cancelText="取消"
    :width="458"
    @confirm="deleteConfirm"
    @cancel="deleteCancel"
  >
    <span>确定要刪除该采集资源点吗？</span></ModalView
  >
</template>

<script lang="ts" setup name="ActionsBtnGroup">
import { computed, ref } from 'vue'
import { useRouter } from 'vue-router'
import { selectedRowKeys } from '../../../../hooks/useTable'
import ModalView from '@/components/components/modal/ModalView.vue'
import {
  startOrDisableStatus,
  deleteResourceSite,
  modalTaskVisible,
  deleteTaskVisible,
  setStartOrDisableRequestApi,
  setDeleteRequestApi
} from '../../../../public-handle'
import apis from '@/apis'
const { updateCollectStatusMultiple, deleteCollectMultiple } = apis.collectManage
const btnVisible = computed(() => !selectedRowKeys.value?.length)
const taskType = ref()
const taskId = ref()
const taskText = ref()
const router = useRouter()

const toAddCollectForm = () => {
  router.push({ name: 'AddEditCollectForm' })
}

const toCollectLogList = () => {
  router.push({ name: 'CollectLogList' })
}
const startOrDisabledBtnTextDefault = computed((): string => {
  if (taskType.value === 2) {
    return '批量启用'
  }
  if (taskType.value === 1) {
    return '批量禁用'
  }
  return ''
})
const startOrDisabledHandle = (type: number, id: any, text: string) => {
  if (type === 2) {
    setStartOrDisableRequestApi(updateCollectStatusMultiple)
    startOrDisableStatus(type, id, '采集资源点')
  } else {
    taskType.value = type
    taskId.value = id
    taskText.value = text
    modalTaskVisible.value = true
  }
}
//禁用确认
const disableConfirm = (value: any) => {
  setStartOrDisableRequestApi(updateCollectStatusMultiple)
  startOrDisableStatus(taskType.value, taskId.value, '采集资源点')
}
//禁用取消
const disableCancel = () => {
  modalTaskVisible.value = false
}
//刪除
const handleDeleteCollect = () => {
  deleteTaskVisible.value = true
}
//批量刪除确认
const deleteConfirm = (value: any) => {
  setDeleteRequestApi(deleteCollectMultiple)
  deleteResourceSite('', '采集资源点')
}
//批量刪除取消
const deleteCancel = () => {
  deleteTaskVisible.value = false
  selectedRowKeys.value = []
}
</script>

<style lang="less" scoped>
.actions-btn-group {
  .ant-btn:not(:first-child) {
    margin-left: 10px;
  }
  display: flex;
  flex-wrap: nowrap;
}
</style>
